﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using TSOL.FastSPWeb.Core;
using TSOL.FastSPWeb.ValueObjects;


namespace TSOL.FastSPWeb
{

    public static partial class FastSPWebCallExtensions
    {
        public static Guid FastCreateSubWeb(this SPWeb Web, string strWebUrl, string strTitle, 
                                            string strDescription, uint nLCID, string strWebTemplate, 
                                            bool useUniquePermissions, bool bConvertIfThere)
        {
            return FastSPWebThreadWorkerQueue.GlobalSPItemUpdateThreadWorkerQueue.QueueCreateSubWeb(Web, strWebUrl, strTitle, strDescription, nLCID, strWebTemplate, useUniquePermissions, bConvertIfThere);
        }

        public static Guid FastUpdate(this SPItem Item, SPList List)
        {
            return FastSPWebThreadWorkerQueue.GlobalSPItemUpdateThreadWorkerQueue.QueueUpdate(List, Item);
        }
    }


    public partial class FastSPWebCall
    {
        public static bool StatusForAction(Guid ActionID)
        {
            bool updateInQueue = false;
            IFastSPUpdateException ActionException = null;
            if (FastSPWebThreadWorkerQueue.GlobalSPWebThreadWorkerQueue.UpdateExceptionDictionary.TryGetValue(ActionID, out ActionException)) {
                throw new SPException("FastUpdate Exception", ActionException.ActionExceptionList[0]);
            }

            lock (FastSPWebThreadWorkerQueue.GlobalSPItemUpdateThreadWorkerQueue.taskQ) {
                updateInQueue = FastSPWebThreadWorkerQueue.GlobalSPItemUpdateThreadWorkerQueue.taskQ.Any(a => a.ActionID == ActionID);
            }

            return updateInQueue;
        }

        public static bool StatusForAction(List<Guid> ActionIDList)
        {
            foreach (Guid ActionID in ActionIDList)
                if (!StatusForAction(ActionID))
                    return false;

            return true;
        }
    
    }

}
